package com.origin.ebuy.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


public final class JdbcUtils {
	
	static String URL;
	static String USER;
	static String PASSWORD;
	
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			initConfig();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 初始化jdbc配置信息
	 * @throws IOException
	 */
	private static void initConfig() throws IOException{
		InputStream stream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
		Properties properties = new Properties();
		properties.load(stream);
		
		URL = properties.getProperty("DBURL");
		USER = properties.getProperty("DBUSER");
		PASSWORD = properties.getProperty("DBPASSWD");
		
		stream.close();
	}
	
	/**
	 * 获取连接
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException{
		return DriverManager.getConnection(URL, USER, PASSWORD);
	}
	
	
	/**
	 * 关闭jdbc
	 * @param set
	 * @param stat
	 * @param con
	 */
	public static void close(ResultSet set,Statement stat,Connection con){
		
		try {
			if(set != null) set.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		try {
			if(stat != null) stat.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		try {
			if(con != null) con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
}
